
package com.jf.cloud.search.listener;

import cn.throwx.canal.gule.model.CanalBinLogResult;
import com.jf.cloud.api.distribution.feign.DistributionFeignClient;
import com.jf.cloud.api.distribution.vo.DistributionSpuVO;
import com.jf.cloud.common.product.bo.EsDistributionInfoBO;
import com.jf.cloud.common.product.bo.EsProductBO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.search.bo.DistributionInfoBO;
import com.jf.cloud.search.util.EsSearchUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;

/**
 * @author zz
 * @Date 2021/8/9 16:55
 */
@Component
public class DistributionSpuListener extends BaseCanalBinlogEventThrowExceptionProcessor<DistributionInfoBO> {

    private static final Logger log = LoggerFactory.getLogger(DistributionSpuListener.class);

    @Autowired
    private DistributionFeignClient distributionFeignClient;

    /**
     *  新增分销商品
     */
    @Override
    protected void processInsertInternal(CanalBinLogResult<DistributionInfoBO> result) {
        updateDistributionSpuInfo(result.getAfterData().getDistributionSpuId());
    }

    /**
     * 更新分销商品
     */
    @Override
    protected void processUpdateInternal(CanalBinLogResult<DistributionInfoBO> result) {
        updateDistributionSpuInfo(result.getAfterData().getDistributionSpuId());
    }

    /**
     * 删除分销商品
     */
    @Override
    protected void processDeleteInternal(CanalBinLogResult<DistributionInfoBO> result) {
        DistributionInfoBO afterData = result.getAfterData();
        EsProductBO esProductBO = new EsProductBO();
        esProductBO.setDistributionSpu(Boolean.FALSE);
        esProductBO.setDistributionInfo(null);
        EsSearchUtil.esUpdateSpuBySpuIds(Collections.singletonList(afterData.getSpuId()), esProductBO);
    }

    /**
     * 更新商品的分销信息
     * @param distributionId
     */
    private void updateDistributionSpuInfo(Long distributionId) {
        ServerResponseEntity<DistributionSpuVO> distributionRes = distributionFeignClient.getByDistributionId(distributionId);
        DistributionSpuVO distributionSpuVO = distributionRes.getData();
        EsProductBO esProductBO = new EsProductBO();
        esProductBO.setDistributionSpu(Boolean.TRUE);

        EsDistributionInfoBO esDistributionInfoBO = new EsDistributionInfoBO();
        esDistributionInfoBO.setDistributionSpuId(distributionSpuVO.getDistributionSpuId());
        esDistributionInfoBO.setState(distributionSpuVO.getState());
        esDistributionInfoBO.setAwardNumbers(distributionSpuVO.getAwardNumbers());
        esDistributionInfoBO.setAwardMode(distributionSpuVO.getAwardMode());
        esDistributionInfoBO.setAwardNumbers(distributionSpuVO.getAwardNumbers());
        esDistributionInfoBO.setParentAwardNumbers(distributionSpuVO.getParentAwardNumbers());
        esDistributionInfoBO.setParentAwardSet(distributionSpuVO.getParentAwardSet());
        esProductBO.setDistributionInfo(esDistributionInfoBO);

        EsSearchUtil.esUpdateSpuBySpuIds(Collections.singletonList(distributionSpuVO.getSpuId()), esProductBO);
    }
}
